<?php
namespace jy_gufee;
defined('BY_JYA') or exit('error');
/*
	任务
*/
class task extends \plugin{
	//计划任务
	public function up_jifen($setting=array()){
		if($setting){
			$this->setting=$setting;
		}
		//昨天
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$lastdaytime = $daytime-24*3600;
		$isadd=$this->get('feelog',array('type'=>1,'uniacid'=>$_SESSION['uniacid'],'createtime >'=>$daytime),'id');
		if($isadd['id']){
			return false;
		}
		$params=array('merchid'=>0,'ispay'=>1,'uniacid'=>$_SESSION['uniacid']);
		if($this->setting['isgoods']){
			$goods_ids=$this->getall('goods',array('goods_id >'=>0,'ignore'=>0,'uniacid'=>$_SESSION['uniacid']),'goods_id','goods_id');
			if($goods_ids){
				$goods_ids=array_keys($goods_ids);
			}else{
				$goods_ids=-1;
			}
			$params['goodsid']=$goods_ids;	
		}
		$params['createtime between']=array($lastdaytime,$daytime);	
		if(!$this->setting['isgoods']){
			$orderprice=pdo_getsum('jy_weishop_order',$params,'price');
		}else{
			$orderprice=pdo_getsum('jy_weishop_order_goods',$params,'total_price');
		}
		$allnum=0;
		if($orderprice>0){
			$levels=$this->getall('level',array('is_day'=>1,'day_rate <>'=>'','uniacid'=>$_SESSION['uniacid']));
			if($levels){
				foreach($levels as $lv){
					$day_rate=$this->setting['day_rate']?:0;
					$day_rate=$lv['day_rate']?:$day_rate;
					if($day_rate){
						$alljifen=$orderprice*$day_rate/100;
					}
					if(!$alljifen){
						continue;
					}
					$members=$this->getall('member',array('is_agent'=>1,'level_id'=>$lv['id'],'uniacid'=>$_SESSION['uniacid']),array('mid'),'mid');
					$num=count($members);
					$allnum +=$num;
					$onejifen=$alljifen/$num;
					if($lv['day_max']>0 && $onejifen>$lv['day_max']){
						$onejifen=$lv['day_max'];
					}
					foreach($members as $mv){
						$new_commission=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$mv['mid'],
							'money'=>$onejifen,
							'createtime'=>TIMESTAMP,
							'status'=>1,
							'type'=>7,
						);
						$this->insert('commission',$new_commission);
					}
				}
			}
		}
		$log=array(
			'num'=>$allnum,
			'orderprice'=>$orderprice,
			'alljifen'=>$alljifen,
			'onejifen'=>$onejifen,
		);
		$this->add_feelog($log,1);
		//上周
		$starttime = mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
		$lastdaytime = $starttime+7*24*3600;
		$isadd=$this->get('feelog',array('type'=>6,'uniacid'=>$_SESSION['uniacid'],'createtime >'=>$lastdaytime),'id');
		if($isadd['id']){
			return false;
		}
		$allnum=0;
		$params['createtime between']=array($starttime,$lastdaytime);	
		if(!$this->setting['isgoods']){
			$orderprice=pdo_getsum('jy_weishop_order',$params,'price');
		}else{
			$orderprice=pdo_getsum('jy_weishop_order_goods',$params,'total_price');
		}
		if($orderprice>0){
		    $week_rate=$this->setting['week_rate']?:0;
		    $levels=$this->getall('level',array('is_week'=>1,'week_rate <>'=>'','uniacid'=>$_SESSION['uniacid']));
			if($levels){
			foreach($levels as $lv){
				$week_rate=$this->setting['week_rate']?:0;
				$week_rate=$lv['week_rate']?:$week_rate;
				if($week_rate){
					$alljifen=$orderprice*$week_rate/100;
				}
				if(!$alljifen){
					continue;
				}
				$members=$this->getall('member',array('is_agent'=>1,'level_id'=>$lv['id'],'uniacid'=>$_SESSION['uniacid']),array('mid'),'mid');
                $num=count($members);
				$allnum +=$num;
				$onejifen=$alljifen/$num;
				if($lv['week_max']>0 && $onejifen>$lv['week_max']){
					$onejifen=$lv['week_max'];
				}
				foreach($members as $mv){
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$mv['mid'],
						'money'=>$onejifen,
						'createtime'=>TIMESTAMP,
						'status'=>1,
						'type'=>14,
					);
					$this->insert('commission',$new_commission);
				}
			}
			}
		}
		$log=array(
			'num'=>$allnum,
			'orderprice'=>$orderprice,
			'alljifen'=>$alljifen,
			'onejifen'=>$onejifen,
		);
		$this->add_feelog($log,6);
		//上月
		$lasttime=strtotime('-1 month');
		$month_start=strtotime(date('Ym').'01');
		$isadd=$this->get('feelog',array('type'=>2,'uniacid'=>$_SESSION['uniacid'],'createtime >'=>$month_start),'id');
		if($isadd['id']){
			return false;
		}
		$allnum=0;
		$params['createtime between']=array($lasttime,$month_start);	
		if(!$this->setting['isgoods']){
			$orderprice=pdo_getsum('jy_weishop_order',$params,'price');
		}else{
			$orderprice=pdo_getsum('jy_weishop_order_goods',$params,'total_price');
		}
		if($orderprice>0){
            $month_rate=$this->setting['month_rate']?:0;
		    $levels=$this->getall('level',array('is_month'=>1,'month_rate <>'=>'','uniacid'=>$_SESSION['uniacid']));
			if($levels){
			foreach($levels as $lv){
				$month_rate=$this->setting['month_rate']?:0;
				$month_rate=$lv['month_rate']?:$month_rate;
				if($month_rate){
					$alljifen=$orderprice*$month_rate/100;
				}
				if(!$alljifen){
					continue;
				}
				$members=$this->getall('member',array('is_agent'=>1,'level_id'=>$lv['id'],'uniacid'=>$_SESSION['uniacid']),array('mid'),'mid');
                $num=count($members);
				$allnum +=$num;
				$onejifen=$alljifen/$num;
				if($lv['month_max']>0 && $onejifen>$lv['month_max']){
					$onejifen=$lv['month_max'];
				}
				foreach($members as $mv){
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$mv['mid'],
						'money'=>$onejifen,
						'createtime'=>TIMESTAMP,
						'status'=>1,
						'type'=>8,
					);
					$this->insert('commission',$new_commission);
				}
			}
			}
		}
		$log=array(
			'num'=>$allnum,
			'orderprice'=>$orderprice,
			'alljifen'=>$alljifen,
			'onejifen'=>$onejifen,
		);
		$this->add_feelog($log,2);
		//上季度
		$season = ceil((date('n'))/3)-1;//上季度是第几季度
		$lasttime=mktime(0, 0, 0,$season*3-3+1,1,date('Y'));
		$endtime=mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'));
		$isadd=$this->get('feelog',array('type'=>3,'uniacid'=>$_SESSION['uniacid'],'createtime >'=>$endtime),'id');
		if($isadd['id']){
			return false;
		}
		$allnum=0;
		$params['createtime between']=array($lasttime,$endtime);	
		if(!$this->setting['isgoods']){
			$orderprice=pdo_getsum('jy_weishop_order',$params,'price');
		}else{
			$orderprice=pdo_getsum('jy_weishop_order_goods',$params,'total_price');
		}
		if($orderprice>0){
		    $levels=$this->getall('level',array('is_jidu'=>1,'jidu_rate <>'=>'','uniacid'=>$_SESSION['uniacid']));
			if($levels){
			foreach($levels as $lv){
				$jidu_rate=$this->setting['jidu_rate']?:0;
				$jidu_rate=$lv['jidu_rate']?:$jidu_rate;
				if($jidu_rate){
					$alljifen=$orderprice*$jidu_rate/100;
				}
				if(!$alljifen){
					continue;
				}
				$members=$this->getall('member',array('is_agent'=>1,'level_id'=>$lv['id'],'uniacid'=>$_SESSION['uniacid']),array('mid'),'mid');
                $num=count($members);
				$allnum +=$num;
				$onejifen=$alljifen/$num;
				if($lv['jidu_max']>0 && $onejifen>$lv['jidu_max']){
					$onejifen=$lv['jidu_max'];
				}
				foreach($members as $mv){
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$mv['mid'],
						'money'=>$onejifen,
						'createtime'=>TIMESTAMP,
						'status'=>1,
						'type'=>8,
					);
					$this->insert('commission',$new_commission);
				}
			}
			}
		}
		$log=array(
			'num'=>$allnum,
			'orderprice'=>$orderprice,
			'alljifen'=>$alljifen,
			'onejifen'=>$onejifen,
		);
		$this->add_feelog($log,3);
		//上年
		$year = date('Y');
		$lastyear = date('Y')-1;
		$lasttime=strtotime($lastyear.'-01');
		$endtime=strtotime($year.'-01');
		$isadd=$this->get('feelog',array('type'=>4,'uniacid'=>$_SESSION['uniacid'],'createtime >'=>$endtime),'id');
		if($isadd['id']){
			return false;
		}
		$allnum=0;
		$params['createtime between']=array($lasttime,$endtime);	
		if(!$this->setting['isgoods']){
			$orderprice=pdo_getsum('jy_weishop_order',$params,'price');
		}else{
			$orderprice=pdo_getsum('jy_weishop_order_goods',$params,'total_price');
		}
		if($orderprice>0){
            $levels=$this->getall('level',array('is_year'=>1,'year_rate <>'=>'','uniacid'=>$_SESSION['uniacid']));
			if($levels){
			foreach($levels as $lv){
				$year_rate=$this->setting['year_rate']?:0;
				$year_rate=$lv['year_rate']?:$year_rate;
				if($year_rate){
					$alljifen=$orderprice*$year_rate/100;
				}
				if(!$alljifen){
					continue;
				}
				$members=$this->getall('member',array('is_agent'=>1,'level_id'=>$lv['id'],'uniacid'=>$_SESSION['uniacid']),array('mid'),'mid');
				$num=count($members);
				$allnum +=$num;
				$onejifen=$alljifen/$num;
				if($lv['year_max']>0 && $onejifen>$lv['year_max']){
					$onejifen=$lv['year_max'];
				}
				foreach($members as $mv){
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$mv['mid'],
						'money'=>$onejifen,
						'createtime'=>TIMESTAMP,
						'status'=>2,
						'type'=>8,
					);
					$this->insert('commission',$new_commission);
				}
			}
			}
		}
		$log=array(
			'num'=>$allnum,
			'orderprice'=>$orderprice,
			'alljifen'=>$alljifen,
			'onejifen'=>$onejifen,
		);
		$this->add_feelog($log,4);
		return true;
	}
	public function add_feelog($log=array(),$type){
		$feelog=array(
			'uniacid'=>$_SESSION['uniacid'],
			'num'=>$log['num'],
			'onejifen'=>$log['onejifen'],
			'alljifen'=>$log['alljifen'],
			'orderprice'=>$log['orderprice'],
			'type'=>$type,
			'createtime'=>TIMESTAMP,
		);
		$this->insert('feelog',$feelog);
		return true;
	}
    public function jifen_sf($setting=array()){
		if($setting){
			$this->setting=$setting;
		}
        if(!$this->setting['sf_open']){
            return false;
        }
        $sf_btype=$this->setting['sf_btype'];
        $daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
        if($this->setting['sf_days']>1){
            $createtime = TIMESTAMP-24*3600*$this->setting['sf_days'];
        }else{
            $createtime=$daytime;
        }
        $isplan = $this->pdo_get('feelog',array('uniacid'=>$_SESSION['uniacid'],'type'=>5,'createtime >'=>$createtime),array('id'));
        if($isplan){
           return false;
        }
        $params=array('uniacid'=>$_SESSION['uniacid'],'mid >'=>0);
        $members = pdo_getall('jy_gufee_member',$params,array('mid'));
        if($members){
			$alljifen=0;
            foreach($members as $item){
                $item['credit1']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),'credit1');
                if($item['credit1'] <=0){
                    continue;
                }
				if($this->setting['sf_man']>0 && $item['credit1']<$this->setting['sf_man']){
                    continue;
                }
                $fee=$this->setting['sf_rate']*$item['credit1']/100;
				$alljifen +=$fee;
				$sf_fee=0;
                if($this->setting['sf_fee']>0){
					//手续费
                    $sf_fee=$fee*$this->setting['sf_fee']/100;
                    $fee -=$sf_fee;
                }
                if($fee > 0){
                    pdo_update('jy_gufee_member',array('bfee +='=>$fee),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
					$allfee=$fee+$sf_fee;
                    model('core/member')->member_credit($item['mid'],'credit1',$allfee,false,'jy_gufee','每日释放积分为银币','isplan');
                    $new_commission=array(
                        'uniacid'=>$_SESSION['uniacid'],
                        'mid'=>$item['mid'],
                        'money'=>0,
                        'jifen'=>$fee,
                        'createtime'=>TIMESTAMP,
                        'status'=>2,
                        'type'=>12,
                    );
                    $this->insert('commission',$new_commission);
                }
            }
        }
		$onejifen=count($members)>0?$alljifen/count($members):0;
        $log=array(
            'num'=>count($members),
			'onejifen'=>$onejifen,
			'alljifen'=>$alljifen,
        );
        $this->add_feelog($log,5);
        return true;
    }
}